package com.lys.spay.service;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.zip.CheckedOutputStream;

@RestController
public class PayService {
    @GetMapping("/payment_config")
    @ApiOperation("元信息")
    public Object getPaymentConfig() {
        return new Object() {
            public String code = "10000";
            public Object msg = new Object() {
                public Object paystr = new Object() {
                    public Object 微信 = new Object() {
                        public boolean isUp = true;
                        public String[] packName = {"com.tencent.mm"};
                        public Object[] list = {
                                new Object() {
                                    public boolean isTitle = false;
                                    public String start = "微信支付收款";
                                    public String end = "元";
                                },
                                new Object() {
                                    public boolean isTitle = false;
                                    public String start = "收款";
                                    public String end = "元";
                                },
                                new Object() {
                                    public boolean isTitle = false;
                                    public String start = "微信收款助手，收款";
                                    public String end = "元";
                                }
                        };
                    };
                };
                public String purl = "http://8.134.189.49:8080/payment_callback";
            };
        };
    }

/*    @PostMapping("/payment_callback")
    public Object processPaymentCallback(@RequestBody CallbackRequest request) {
        String priKey = "lys"; // 请替换为实际的私钥

        // 验证priKey和time
        long currentTime = System.currentTimeMillis() / 1000; // 当前时间戳（秒）
        if (!priKey.equals(request.getPriKey()) || currentTime - request.getTime() > 150) {
            return new Response("20001", "权限验证失败");
        }

        // 根据amount和type确认订单，完成支付逻辑
        // 这里可以添加具体的订单处理逻辑

        // 响应结果
        return new Response("10000", "支付回调成功");
    }*/

    @PostMapping("/payment_callback")
    @ApiOperation("支付回调")
    public Object processPaymentCallback( @RequestParam int amount, @RequestParam String type, @RequestParam String priKey, @RequestParam long time) {
        String Key = "lys"; // 请替换为实际的私钥
        System.out.println("amount:"+amount);
        System.out.println("priKey:"+priKey);
        System.out.println("time:"+time);
        System.out.println("type:"+type);

        // 验证priKey和time
        long currentTime = System.currentTimeMillis() / 1000; // 当前时间戳（秒）
        if (!Key.equals(priKey) || currentTime - time > 150) {
            return new Response("20001", "权限验证失败");
        }
        // 根据amount和type确认订单，完成支付逻辑
        // 这里可以添加具体的订单处理逻辑
        // 响应结果
        return new Response("10000", "支付成功");
    }

    static class CallbackRequest {
        private int amount;
        private String type;
        private String priKey;
        private long time;

        // 省略getter和setter方法

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getPriKey() {
            return priKey;
        }

        public void setPriKey(String priKey) {
            this.priKey = priKey;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }
    }

    static class Response {
        private String code;
        private String msg;

        public Response(String code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        // 省略getter和setter方法

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
